import request from "../utils/request";
import store from "../store";
import {Observable} from "rxjs";

export function getTransactions(page, pageSize, cardNo, filter, dateRange) {
  const params = {page, pageSize, cardNo, filter, from: null, to: null};
  if (dateRange && dateRange.length === 2) {
    params.from = dateRange[0];
    params.to = dateRange[1];
  }
  return request.get('/transactions', {
    params: params,
  })
}

export function addTransaction(to, amount, remark) {
  const fromCredential = store.getters.card.auth;
  return new Observable(subscriber => {
    if (!fromCredential) subscriber.next(null);
    else request.post('/transactions', {
      to, amount, fromCredential, remark
    }).subscribe(
      res => subscriber.next(res),
      err => subscriber.error(err),
      () => subscriber.complete()
    );
  })
}
